package com.lwz.eduservice.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.lwz.eduservice.entity.EduTeacher;
import com.lwz.eduservice.entity.vo.TeacherQuery;
import com.lwz.eduservice.service.EduTeacherService;
import com.lwz.util.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 讲师 前端控制器
 * </p>
 *
 * @author Lw中
 * @since 2020-11-03
 */
@Api(value = "讲师管理")
@RestController
@RequestMapping("/eduservice/teacher")
@CrossOrigin
public class EduTeacherController {

    @Autowired(required = false)
    EduTeacherService eduTeacherService;

    /**
     * 查询所有较教师信息
     */
    @ApiOperation(value = "所有讲师列表")
    @GetMapping("/findAll")
    public R findAll() {
        List<EduTeacher> list = eduTeacherService.list(null);
        // 使用链式编程：在调用ok方法之后，在R实体中再添加上list
        return R.ok().data("items", list);
    }

    /**
     * 逻辑删除操作
     */
    @ApiOperation(value = "逻辑删除讲师")
    @DeleteMapping("/{id}")
    public R deleteById(@ApiParam(name = "id", value = "讲师ID", required = true)
                            @PathVariable String id) {
        boolean flag = eduTeacherService.removeById(id);
        System.out.println(flag);
        if (flag) {
            return R.ok();
        } else {
            return R.error().message("删除失败");
        }
    }

    /**
     * 讲师数据分页查询
     */
    @ApiOperation(value = "讲师分页查询")
    @GetMapping("/pageTeacher/{current}/{limit}")
    public R pageListTeacher(
            @ApiParam(name = "current", value = "当前页数", required = true)
            @PathVariable long current,
            @ApiParam(name = "limit", value = "每页条数", required = true)
            @PathVariable long limit) {
        // 创建page对象
        Page<EduTeacher> pageTeacher = new Page<>(current, limit);
        // page方法将讲师的数据全部存储在pageTeacher对象中
        eduTeacherService.page(pageTeacher, null);
        // 获取所有讲师数据
        List<EduTeacher> records = pageTeacher.getRecords();
        // 获取所有数据的总条数
        long total = pageTeacher.getTotal();
        return R.ok().data("total", total).data("rows", records);
    }

    /**
     * 讲师数据条件查询
     */
    @ApiOperation(value = "条件分页查询")
    @PostMapping("/pageTeacherCondition/{current}/{limit}")
    public R pageTeacherCondition(
            @ApiParam(name = "current", value = "当前页数", required = true)
            @PathVariable long current,

            @ApiParam(name = "limit", value = "每页条数", required = true)
            @PathVariable long limit,

            @ApiParam(name = "teacherQuery", value = "查询对象", required = false)
            @RequestBody(required = false) TeacherQuery teacherQuery) {
        //创建page对象
        Page<EduTeacher> pageTeacher = new Page<>(current,limit);

        //构建条件
        QueryWrapper<EduTeacher> wrapper = new QueryWrapper<>();

        // 测试统一异常处理
//        try {
//            int a = 10/0;
//        } catch (Exception e) {
//            throw new MyException(20001, "自定义异常处理");
//        }

        // 多条件组合查询
        // mybatis学过 动态sql
        String name = teacherQuery.getName();
        Integer level = teacherQuery.getLevel();
        String begin = teacherQuery.getBegin();
        String end = teacherQuery.getEnd();
        //判断条件值是否为空，如果不为空拼接条件
        if(!StringUtils.isEmpty(name)) {
            //构建条件
            wrapper.like("name",name);
        }
        if(!StringUtils.isEmpty(level)) {
            wrapper.eq("level",level);
        }
        if(!StringUtils.isEmpty(begin)) {
            wrapper.ge("gmt_create",begin);
        }
        if(!StringUtils.isEmpty(end)) {
            wrapper.le("gmt_create",end);
        }

        //排序
        wrapper.orderByDesc("gmt_create");

        //调用方法实现条件查询分页
        eduTeacherService.page(pageTeacher,wrapper);
        //总记录数
        long total = pageTeacher.getTotal();
        //数据list集合
        List<EduTeacher> records = pageTeacher.getRecords();
        return R.ok().data("total",total).data("rows",records);
    }

    @ApiOperation(value = "新增讲师")
    @PostMapping("/addTeacher")
    public R addTeacher(@ApiParam(name = "eduTeacher", value = "讲师对象", required = true)
                        @RequestBody EduTeacher eduTeacher) {
        boolean save = eduTeacherService.save(eduTeacher);
        if (save) {
            return R.ok();
        } else {
            return R.error().message("添加失败");
        }
    }

    /**
     * 根据讲师id进行查询
     * @param id
     * @return
     */
    @ApiOperation(value = "id查询讲师")
    @GetMapping("getTeacher/{id}")
    public R getTeacher(
            @ApiParam(name = "id", value = "讲师id", required = true)
            @PathVariable String id) {
        EduTeacher eduTeacher = eduTeacherService.getById(id);
        return R.ok().data("teacher",eduTeacher);
    }

    /**
     * 讲师修改功能
     * @param eduTeacher
     * @return
     */
    @ApiOperation(value = "修改讲师数据")
    @PostMapping("updateTeacher")
    public R updateTeacher(
            @ApiParam(name = "eduTeacher", value = "讲师对象", required = true)
            @RequestBody EduTeacher eduTeacher) {
        boolean flag = eduTeacherService.updateById(eduTeacher);
        if(flag) {
            return R.ok();
        } else {
            return R.error().message("修改失败");
        }
    }

}

